<?php
include "inc/db_connect.php";
include "inc/functions.php";
session_start();
if (isset($_POST['zero']) && isset($_POST['oddelenie']) && isset($_POST['tyzden'])) {
    $veduci_oddelenia = $_POST['oddelenie'];
    $bodNula = $_POST['zero'];
    $tyzden = $_POST['tyzden'];
    $filename = "week_" . date("dmY", $bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60)) . '_' . date("dmY", $bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60) + (6 * 24 * 60 * 60)) . ".xls";
    header("http-equiv='content-type' content='text/html; charset=windows-1250'");
    header("Content-type: application/vnd.ms-excel");
    header("Content-Disposition: attachment; filename=" . $filename);
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0,pre-check=0");
    header("Pragma: public");
    $block = false;
    $dochMesiac = date("n", ($bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60)));
    $dochMesiac2 = date("n", ($bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60) + (6 * 24 * 60 * 60)));
    $desiatyDen = date("U", mktime(0, 0, 0, $dochMesiac + 1, 10, date("Y")));
//echo '/';
    $dnesnyDen = date("U");
    $block = block($dochMesiac, $dochMesiac2, $desiatyDen, $dnesnyDen);
    $upravaPoradia = false;
    ?>
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="sk" lang="sk">
        <head>

            <meta http-equiv="content-language" content="sk" />
            <meta http-equiv="cache-control" content="no-cache" />
            <meta http-equiv="content-type" content="text/html; charset=utf-8" />
            <title>BMB Leitner CS</title>
            <style type="">
                th { text-align: center !important; vertical-align: middle !important; }
                td { text-align: center !important; vertical-align: middle !important; }
            </style>
        </head>
        <body>
            <table style="border-collapse: collapse; font-size: 13px; text-align: center; vertical-align: middle; width: 1550px">
                <tr>
                    <?php
                    for ($i = 0; $i < 23; $i++) {
                        echo '<td style="width:70px; height: 20px;"></td>';
                    }
                    ?>
                </tr>
                <tr>
                    <td colspan="23">
                        <?php
                        $odd_s = "SELECT * FROM oddelenie WHERE id='" . $veduci_oddelenia . "' ";
                        $odd_q = MySQL_Query($odd_s);
                        $odd = MySQL_Fetch_Array($odd_q);
                        echo '<span style="font-size: 20px;">Oddelenie: <b>' . $odd['nazov'] . '</b></span><br/><br/>';
                        echo '<span style="font-size: 16px;">Týždeň od <b>' . date("d.m.Y", $bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60)) . ' do ' . date("d.m.Y", $bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60) + (6 * 24 * 60 * 60)) . '</b></span>';
                        ?>
                    </td>
                </tr>
                <tr>
                    <?php
                    for ($i = 0; $i < 23; $i++) {
                        echo '<td style="width:70px; height: 20px;"></td>';
                    }
                    ?>
                </tr>
                <tr style="background: #e4c9ff">
                    <th style="border: 1px solid black" rowspan="2" colspan="1">Poradie</th>
                    <th style="border: 1px solid black" rowspan="2" colspan="2">Meno</th>
                    <th style="border: 1px solid black" rowspan="2" colspan="1">Nočných k <?php echo date("d.m. Y", ($bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60) - (1 * 24 * 60 * 60))); ?></th>
                    <th style="border: 1px solid black" rowspan="2" colspan="1">Sviatkov k <?php echo date("d.m. Y", ($bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60) - (1 * 24 * 60 * 60))); ?></th>
                    <th style="border: 1px solid black" rowspan="2" colspan="1">Strava k <?php echo date("d.m. Y", ($bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60) - (1 * 24 * 60 * 60))); ?></th>
                    <th style="border: 1px solid black" rowspan="2" colspan="1">Nadčas k <?php echo date("d.m. Y", ($bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60) - (1 * 24 * 60 * 60))); ?></th>
                    <th style="border: 1px solid black" rowspan="2" colspan="1">Mod k <?php echo date("d.m. Y", ($bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60) - (1 * 24 * 60 * 60))); ?></th>
                    <th style="border: 1px solid black" rowspan="2" colspan="1">&nbsp;</th>
                    <th style="border: 1px solid black<?php if (jeSviatok(($bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60) + (0 * 24 * 60 * 60))))
                    echo '; background: yellow' ?>" rowspan="1" colspan="2"><?php echo date("d-M", ($bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60) + (0 * 24 * 60 * 60))) ?></th>
                    <th style="border: 1px solid black<?php if (jeSviatok(($bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60) + (1 * 24 * 60 * 60))))
                        echo '; background: yellow' ?>" rowspan="1" colspan="2"><?php echo date("d-M", ($bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60) + (1 * 24 * 60 * 60))) ?></th>
                    <th style="border: 1px solid black<?php if (jeSviatok(($bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60) + (2 * 24 * 60 * 60))))
                        echo '; background: yellow' ?>" rowspan="1" colspan="2"><?php echo date("d-M", ($bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60) + (2 * 24 * 60 * 60))) ?></th>
                    <th style="border: 1px solid black<?php if (jeSviatok(($bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60) + (3 * 24 * 60 * 60))))
                        echo '; background: yellow' ?>" rowspan="1" colspan="2"><?php echo date("d-M", ($bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60) + (3 * 24 * 60 * 60))) ?></th>
                    <th style="border: 1px solid black<?php if (jeSviatok(($bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60) + (4 * 24 * 60 * 60))))
                        echo '; background: yellow' ?>" rowspan="1" colspan="2"><?php echo date("d-M", ($bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60) + (4 * 24 * 60 * 60))) ?></th>
                    <th style="border: 1px solid black<?php if (jeSviatok(($bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60) + (5 * 24 * 60 * 60))))
                        echo '; background: yellow' ?>" rowspan="1" colspan="2"><?php echo date("d-M", ($bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60) + (5 * 24 * 60 * 60))) ?></th>
                    <th style="border: 1px solid black<?php if (jeSviatok(($bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60) + (6 * 24 * 60 * 60))))
                        echo '; background: yellow' ?>" rowspan="1" colspan="2"><?php echo date("d-M", ($bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60) + (6 * 24 * 60 * 60))) ?></th>
                </tr>
                <tr style="background: #e4c9ff">
                    <td style="border: 1px solid black<?php if (jeSviatok(($bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60) + (0 * 24 * 60 * 60))))
                        echo '; background: yellow' ?>" rowspan="1" colspan="1">príchod</td>
                    <td style="border: 1px solid black<?php if (jeSviatok(($bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60) + (0 * 24 * 60 * 60))))
                    echo '; background: yellow' ?>" rowspan="1" colspan="1">odchod</td>
                    <td style="border: 1px solid black<?php if (jeSviatok(($bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60) + (1 * 24 * 60 * 60))))
                    echo '; background: yellow' ?>" rowspan="1" colspan="1">príchod</td>
                    <td style="border: 1px solid black<?php if (jeSviatok(($bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60) + (1 * 24 * 60 * 60))))
                    echo '; background: yellow' ?>" rowspan="1" colspan="1">odchod</td>
                    <td style="border: 1px solid black<?php if (jeSviatok(($bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60) + (2 * 24 * 60 * 60))))
                    echo '; background: yellow' ?>" rowspan="1" colspan="1">príchod</td>
                    <td style="border: 1px solid black<?php if (jeSviatok(($bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60) + (2 * 24 * 60 * 60))))
                    echo '; background: yellow' ?>" rowspan="1" colspan="1">odchod</td>
                    <td style="border: 1px solid black<?php if (jeSviatok(($bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60) + (3 * 24 * 60 * 60))))
                    echo '; background: yellow' ?>" rowspan="1" colspan="1">príchod</td>
                    <td style="border: 1px solid black<?php if (jeSviatok(($bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60) + (3 * 24 * 60 * 60))))
                    echo '; background: yellow' ?>" rowspan="1" colspan="1">odchod</td>
                    <td style="border: 1px solid black<?php if (jeSviatok(($bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60) + (4 * 24 * 60 * 60))))
                    echo '; background: yellow' ?>" rowspan="1" colspan="1">príchod</td>
                    <td style="border: 1px solid black<?php if (jeSviatok(($bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60) + (4 * 24 * 60 * 60))))
                    echo '; background: yellow' ?>" rowspan="1" colspan="1">odchod</td>
                    <td style="border: 1px solid black<?php if (jeSviatok(($bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60) + (5 * 24 * 60 * 60))))
                    echo '; background: yellow' ?>" rowspan="1" colspan="1">príchod</td>
                    <td style="border: 1px solid black<?php if (jeSviatok(($bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60) + (5 * 24 * 60 * 60))))
                    echo '; background: yellow' ?>" rowspan="1" colspan="1">odchod</td>
                    <td style="border: 1px solid black<?php if (jeSviatok(($bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60) + (6 * 24 * 60 * 60))))
                    echo '; background: yellow' ?>" rowspan="1" colspan="1">príchod</td>
                    <td style="border: 1px solid black<?php if (jeSviatok(($bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60) + (6 * 24 * 60 * 60))))
                    echo '; background: yellow' ?>" rowspan="1" colspan="1">odchod</td>
                </tr>
                <tr>
                    <?php
                    for ($i = 0; $i < 23; $i++) {
                        echo '<td style="width:70px; height: 5px;"></td>';
                    }
                    ?>
                </tr>
                <?php
                $zam_s = "SELECT * FROM z WHERE patri_k='" . $veduci_oddelenia . "' ORDER BY poradie ASC ";
                $zam_q = MySQL_Query($zam_s);
                $bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60);
                $iii = 0;
                while ($zam = MySQL_Fetch_Array($zam_q)) {
                    $odchod = explode('-', $zam['datum_odchodu']);
                    $prichod = explode('-', $zam['datum_prichodu']);
                    if ((((date("U", mktime(0, 0, 0, $odchod[1], $odchod[2], $odchod[0]))) >= ($bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60))) || ($zam['datum_odchodu'] == '0000-00-00')) && ((date("U", mktime(0, 0, 0, $prichod[1], $prichod[2], $prichod[0]))) <= ($bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60) + (6 * 24 * 60 * 60)))) {
                        $iii++;
                        ?>
                        <tr style="background:<?php
            if ($iii % 2 == 0) {
                echo '#ffffff';
            } else {
                echo '#eaeaea';
            }
                        ?>">
                            <td style="border: 1px solid black" rowspan="3" colspan="1">
                                <?php
                                if ($zam['poradie'] == 0) {
                                    echo $iii;
                                } else {
                                    echo $zam['poradie'];
                                }
                                ?>
                            </td>
                            <td style="border: 1px solid black; background: <?php
                    if ($zam['manager'] == 1)
                        echo '#9beffe'; elseif ($zam['manager'] == 0)
                        echo '#e1f6fa'; else
                        echo '#c9e1e5'
                                    ?>" rowspan="3" colspan="2"><?php echo $zam['meno'] . ' ' . $zam['priezvisko'] ?></td>
                            <td style="border: 2px solid black;" rowspan="3" colspan="1" id="nocna<?php echo $zam['id'] ?>">
                                <?php
                                if ($zam['manager'] != 1) {
                                    $vypis = 0;
                                    $sum = vypocitajFinalnyNocnyKtyzdnu($zam['id'], ($bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60) - (1 * 24 * 60 * 60)));
                                    $vypis += $sum;
                                    echo $vypis;
                                } else {
                                    echo '-';
                                }
                                ?>
                            </td>
                            <td style="border: 2px solid black;" rowspan="3" colspan="1" id="sviatok<?php echo $zam['id'] ?>">
                                <?php
                                if ($zam['manager'] != 1) {
                                    $vypis = 0;
                                    $sum = vypocitajFinalnySviatokKtyzdnu($zam['id'], ($bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60) - (1 * 24 * 60 * 60)));
                                    $vypis += $sum;
                                    echo $vypis;
                                } else {
                                    echo '-';
                                }
                                ?>
                            </td>
                            <td style="border: 2px solid black;" rowspan="3" colspan="1" id="strava<?php echo $zam['id'] ?>">
                                <?php
                                $vypis = 0;
                                $vypis += vypocitajFinalnuStravuKtyzdnu($zam['id'], ($bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60) - (1 * 24 * 60 * 60)));
                                echo $vypis;
                                ?>
                            </td>
                            <td style="border: 2px solid black;" rowspan="3" colspan="1" id="nadcas<?php echo $zam['id'] ?>">
                                <?php
                                if ($zam['manager'] != 2) {
                                    $vypis = 0;
                                    $sum = vypocitajFinalnyNadcasKtyzdnu($zam['id'], ($bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60) - (1 * 24 * 60 * 60)));
                                    $vypis += $sum;
                                    echo $vypis;
                                } else {
                                    $vypis = 0;
                                    $sum = pocetOdpracovanychHodinKtyzdnu($zam['id'], ($bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60) - (1 * 24 * 60 * 60)));
                                    $vypis += $sum;
                                    echo $vypis;
                                }
                                ?>
                            </td>
                            <td style="border: 2px solid black;" rowspan="3" colspan="1" id="mod<?php echo $zam['id'] ?>">
                                <?php
                                if ($zam['manager'] == 1) {
                                    $vypis = 0;
                                    $sum = vypocitajFinalnyModKtyzdnu($zam['id'], ($bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60) - (1 * 24 * 60 * 60)));
                                    $vypis += $sum;
                                    echo $vypis;
                                } else {
                                    echo '-';
                                }
                                //echo vypocitatMOD($zam['id'], $bodNula + ($tyzden-1)*(7*24*60*60));
                                ?>
                            </td>
                            <td style="border: 1px solid black;height: 20px" rowspan="1" colspan="1"><?php echo ($zam['fond'] / 5) ?> h./deň </td>
                            <?php
                            for ($k = 0; $k < 7; $k++) {
                                ?>
                                <td style="border: 1px solid black;<?php if (jeSviatok(($bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60) + ($k * 24 * 60 * 60))))
                    echo '; background: yellow' ?>" rowspan="1" colspan="2">
                                        <?php
                                        //echo typDochadzky($zam['id'], ($bodNula + ($tyzden-1)*(7*24*60*60) + ($k*24*60*60)));
                                        ?>
                                </td>
                                <?php
                            }
                            ?>

                        </tr>
                        <tr style="background:<?php
                if ($iii % 2 == 0) {
                    echo '#ffffff';
                } else {
                    echo '#eaeaea';
                }
                            ?>">
                            <td style="border: 1px solid black; height: 20px" rowspan="1" colspan="1">plánované</td>
                            <?php
                            for ($l = 0; $l < 7; $l++) {
                                $in = typDochadzky($zam['id'], ($bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60) + ($l * 24 * 60 * 60)));
                                ?>
                                <td style="border: 1px solid black; background: <?php
                if ($in != 'P')
                    echo $farby[array_search($in, $hodnoty2)]; elseif (jeSviatok(($bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60) + ($l * 24 * 60 * 60))))
                    echo 'yellow'; else
                    echo 'transparent';
                                ?>" rowspan="1" colspan="1" class="day<?php echo $zam['id'] ?><?php echo ($bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60) + ($l * 24 * 60 * 60)) ?>">
                                    <?php
                                    if (($in == 'P') || ($in == 'SPL'))
                                        echo vyplnenyCas($zam['id'], ($bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60) + ($l * 24 * 60 * 60)), 1); else
                                        echo $in;
                                    ?>
                                    <?php
                                    if (($in == 'SPL')) {
                                        echo '<br/>';
                                        echo vyplnenyCas($zam['id'], ($bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60) + ($l * 24 * 60 * 60)), 5);
                                    }
                                    ?>
                                </td>
                                <td style="border: 1px solid black; background: <?php
                    if ($in != 'P')
                        echo $farby[array_search($in, $hodnoty2)]; elseif (jeSviatok(($bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60) + ($l * 24 * 60 * 60))))
                        echo 'yellow'; else
                        echo 'transparent';
                                    ?>" rowspan="1" colspan="1" class="day<?php echo $zam['id'] ?><?php echo ($bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60) + ($l * 24 * 60 * 60)) ?>">
                                    <?php
                                    if (($in == 'P') || ($in == 'SPL'))
                                        echo vyplnenyCas($zam['id'], ($bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60) + ($l * 24 * 60 * 60)), 2); else
                                        echo $in;
                                    ?>
                                    <?php
                                    if (($in == 'SPL')) {
                                        echo '<br/>';
                                        echo vyplnenyCas($zam['id'], ($bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60) + ($l * 24 * 60 * 60)), 6);
                                    }
                                    ?>
                                </td>
                                <?php
                            }
                            ?>
                        </tr>
                        <tr style="background:<?php
                if ($iii % 2 == 0) {
                    echo '#ffffff';
                } else {
                    echo '#eaeaea';
                }
                            ?>">
                            <td style="border: 1px solid black; height: 20px" rowspan="1" colspan="1"><b>reálne</b></td>
                            <?php
                            for ($l = 0; $l < 7; $l++) {
                                $in = typDochadzky($zam['id'], ($bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60) + ($l * 24 * 60 * 60)));
                                ?>
                                <td style="border: 1px solid black; background: <?php
                if ($in != 'P')
                    echo $farby[array_search($in, $hodnoty2)]; elseif (jeSviatok(($bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60) + ($l * 24 * 60 * 60))))
                    echo 'yellow'; else
                    echo 'transparent';
                                ?>" rowspan="1" colspan="1" class="day<?php echo $zam['id'] ?><?php echo ($bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60) + ($l * 24 * 60 * 60)) ?>"><?php
                    if (($in == 'P') || ($in == 'SPL'))
                        echo vyplnenyCas($zam['id'], ($bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60) + ($l * 24 * 60 * 60)), 3); else
                        echo $in;
                                ?></td>
                                <td style="border: 1px solid black; background: <?php
                    if ($in != 'P')
                        echo $farby[array_search($in, $hodnoty2)]; elseif (jeSviatok(($bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60) + ($l * 24 * 60 * 60))))
                        echo 'yellow'; else
                        echo 'transparent';
                                ?>" rowspan="1" colspan="1" class="day<?php echo $zam['id'] ?><?php echo ($bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60) + ($l * 24 * 60 * 60)) ?>"><?php
                    if (($in == 'P') || ($in == 'SPL'))
                        echo vyplnenyCas($zam['id'], ($bodNula + ($tyzden - 1) * (7 * 24 * 60 * 60) + ($l * 24 * 60 * 60)), 4); else
                        echo $in;
                                ?></td>
                                <?php
                            }
                            ?>
                        </tr>
                        <tr>
                            <?php
                            for ($i = 0; $i < 23; $i++) {
                                echo '<td style="width:50px; height: 5px;"></td>';
                            }
                            ?>
                        </tr>
                        <?php
                    }
                }
                ?>
            </table>
            <!-- legenda -->
            <table>
                <?php
                for ($l = 1; $l < count($hodnoty3); $l++) {
                    echo '<tr style="background: ' . $farby[$l] . '"><td>' . $hodnotyy[$l] . '</td><td>' . $hodnoty3[$l] . '</td></tr>';
                }
                ?>
            </table>
        </body>
    </html>
    <?php
}
?>